package com.ruoyi.order.service.impl;

import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.MyUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.employee.domain.EmployeeFinance;
import com.ruoyi.employee.domain.EmployeeStatic;
import com.ruoyi.employee.mapper.EmployeeFinanceMapper;
import com.ruoyi.employee.mapper.EmployeeStaticMapper;
import com.ruoyi.market.domain.Level;
import com.ruoyi.market.mapper.LevelMapper;
import com.ruoyi.order.domain.OrderPk;
import com.ruoyi.order.mapper.OrderPkMapper;
import com.ruoyi.order.service.IOrderPkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.validation.Validator;
import java.math.BigDecimal;
import java.util.List;

/**
 * PK订单 业务层处理
 * 
 * @author ruoyi
 */
@Service
public class OrderPkServiceImpl implements IOrderPkService
{
    @Autowired
    private OrderPkMapper orderPkMapper;

    @Autowired
    private LevelMapper levelMapper;

    @Autowired
    private EmployeeStaticMapper employeeStaticMapper;

    @Autowired
    private EmployeeFinanceMapper employeeFinanceMapper;

    @Autowired
    protected Validator validator;

    /**
     * 根据条件分页查询PK订单列表
     * 
     * @param orderPk PK订单信息
     * @return PK订单信息集合信息
     */
    @Override
    public List<OrderPk> selectOrderPkList(OrderPk orderPk)
    {
        return orderPkMapper.selectOrderPkList(orderPk);
    }

    /**
     * 通过PK订单编号查询PK订单
     * 
     * @param orderNo PK订单编号
     * @return PK订单对象信息
     */
    @Override
    public OrderPk selectOrderPkByOrderNo(String orderNo)
    {
        return orderPkMapper.selectOrderPkByOrderNo(orderNo);
    }

    /**
     * 通过店员ID查询PK订单
     *
     * @param userId 店员ID
     * @return PK订单对象信息
     */
    @Override
    public OrderPk selectOrderPkByUserId(Long userId)
    {
        return orderPkMapper.selectOrderPkByUserId(userId);
    }

    /**
     * 查询店员在待服务和服务中的订单数量
     *
     * @param employeeUserId 店员Id
     * @return 结果
     */
    @Override
    public Integer countOrderPkByEmployeeId(Long employeeUserId)
    {
        return orderPkMapper.countOrderPkByEmployeeId(employeeUserId);
    }

    /**
     * 新增保存PK订单信息
     * 
     * @param orderPk PK订单信息
     * @return 结果
     */
    @Override
    @Transactional
    public OrderPk insertOrderPk(OrderPk orderPk)
    {
        Long userId = SecurityUtils.getUserId();
        String orderNo = MyUtils.getOrderNoByTimeAndId(userId);
        orderPk.setOrderNo(orderNo);
        orderPkMapper.insertOrderPk(orderPk);
        return orderPk;
    }


    /**
     * 取消PK订单信息
     *
     * @param orderPk PK订单信息
     * @return 结果
     */
    @Override
    public int cancelOrderPk(OrderPk orderPk)
    {
        OrderPk order =  orderPkMapper.selectOrderPkByOrderNo(orderPk.getOrderNo());
        if(order != null && order.getPkStatus().equals(UserConstants.STATUS_PREPARING)){
            orderPk.setPkStatus(UserConstants.STATUS_CANCEL);
        }else{
            throw new ServiceException("取消PK订单失败：当前状态不可取消");
        }
        return orderPkMapper.updateOrderPk(orderPk);
    }

    /**
     * 修改保存PK订单信息
     * 
     * @param orderPk PK订单信息
     * @return 结果
     */
    @Override
    public int updateOrderPk(OrderPk orderPk)
    {
        return orderPkMapper.updateOrderPk(orderPk);
    }


    /**
     * 修改保存PK订单信息 - 进度信息
     *
     * @return 结果
     */
    @Override
    public void updateOrderPkProgram()
    {
        orderPkMapper.updateOrderPkProgram();
    }


    /**
     * 修改保存PK订单信息 - 结算
     *
     * @return 结果
     */
    @Override
    public void updateOrderPkSettle()
    {
        OrderPk orderPk = new OrderPk();
        orderPk.setIsSettle(UserConstants.NO);
        orderPk.setPkStatus(UserConstants.STATUS_SUCCESS);
        List<OrderPk> orderPkList =  orderPkMapper.selectOrderPkList(orderPk);
        for(OrderPk o : orderPkList){
            Level start_level = levelMapper.selectLevelById(o.getStartLevelId());
            Level accept_level = levelMapper.selectLevelById(o.getAcceptLevelId());
            EmployeeStatic start_static = employeeStaticMapper.selectUserById(o.getStartUserId());
            EmployeeStatic accept_static = employeeStaticMapper.selectUserById(o.getAcceptUserId());
            BigDecimal start_money = new BigDecimal(0);
            BigDecimal accept_money = new BigDecimal(0);
            if(o.getStartUserAmount().compareTo(o.getAcceptUserAmount()) < 0){
                // 发起者输了 接收者赢了
                start_money = o.getStartUserAmount().multiply(BigDecimal.valueOf(start_level.getPkDefeatCommissionRate())).divide(BigDecimal.valueOf(100));
                accept_money = o.getAcceptUserAmount().multiply(BigDecimal.valueOf(accept_level.getPkVictoryCommissionRate())).divide(BigDecimal.valueOf(100));
            }else if(o.getStartUserAmount().compareTo(o.getAcceptUserAmount()) > 0){
                // 发起者赢了 接收者输了
                start_money = o.getStartUserAmount().multiply(BigDecimal.valueOf(start_level.getPkVictoryCommissionRate())).divide(BigDecimal.valueOf(100));
                accept_money = o.getAcceptUserAmount().multiply(BigDecimal.valueOf(accept_level.getPkDefeatCommissionRate())).divide(BigDecimal.valueOf(100));
            }else if(o.getStartUserAmount().compareTo(o.getAcceptUserAmount()) == 0){
                // 发起者 = 接收者
                start_money = o.getStartUserAmount().multiply(BigDecimal.valueOf(start_level.getPkDefeatCommissionRate())).divide(BigDecimal.valueOf(100));
                accept_money = o.getAcceptUserAmount().multiply(BigDecimal.valueOf(accept_level.getPkDefeatCommissionRate())).divide(BigDecimal.valueOf(100));
            }
            start_static.setPkAmount(start_static.getPkAmount().add(start_money));
            accept_static.setPkAmount(accept_static.getPkAmount().add(accept_money));
            employeeStaticMapper.updateUser(start_static);
            employeeStaticMapper.updateUser(accept_static);

            EmployeeFinance start_finance = new EmployeeFinance();
            start_finance.setShopId(o.getShopId());
            start_finance.setFinanceType(UserConstants.FINANCE_PK);
            start_finance.setOrderNo(o.getOrderNo());
            start_finance.setAmount(start_money);
            start_finance.setUserId(o.getStartUserId());
            employeeFinanceMapper.insertEmployeeFinance(start_finance);

            EmployeeFinance accept_finance = new EmployeeFinance();
            accept_finance.setShopId(o.getShopId());
            accept_finance.setFinanceType(UserConstants.FINANCE_PK);
            accept_finance.setOrderNo(o.getOrderNo());
            accept_finance.setAmount(accept_money);
            accept_finance.setUserId(o.getAcceptUserId());
            employeeFinanceMapper.insertEmployeeFinance(accept_finance);

            orderPk.setIsSettle(UserConstants.YES);
            orderPkMapper.updateOrderPk(orderPk);
        }
    }

    /**
     * 通过PK订单编号删除PK订单
     * 
     * @param orderNo PK订单编号
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteOrderPkByOrderNo(String orderNo)
    {
        return orderPkMapper.deleteOrderPkByOrderNo(orderNo);
    }

    /**
     * 批量删除PK订单信息
     * 
     * @param orderNos 需要删除的PK订单编号
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteOrderPkByOrderNos(String[] orderNos)
    {
        return orderPkMapper.deleteOrderPkByOrderNos(orderNos);
    }
}
